﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Security;
using Tizer.Core;
using Tizer.Core.Enums;
using Tizer.Core.Repository;

namespace Tizer.Web.Providers
{
    /// <summary>
    /// Провайдер ролей
    /// </summary>
    public class TizerRoleProvider : RoleProvider
    {
        private AccountRepository _accountRepository;
        public AccountRepository AccountRepository
        {
            get { return _accountRepository ?? (_accountRepository = new AccountRepository()); }
        }

        // Название приложения
        private string _applicationName = ConfigurationManager.AppSettings["ApplicationName"];
        public override string ApplicationName
        {
            get
            {
                return _applicationName;
            }
            set
            {
                _applicationName = value;
            }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        // получить роли для пользователя
        public override string[] GetRolesForUser(string username)
        {
            if (!string.IsNullOrEmpty(username))
            {
                var roles = new List<string> {AccountType.Guest.ToString()};

                Account account = AccountRepository.GetByLogin(username);
                if (account != null)
                {
                  roles.Add(account.AccountType.ToString());  
                }

                return roles.ToArray();
            }

            return new List<string> {}.ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Проверить аккаунт на принадлежность к роли
        /// </summary>
        /// <param name="username"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override bool IsUserInRole(string username, string roleName)
        {
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(roleName))
            {
                Account account = AccountRepository.GetByLogin(username);
                if (account != null)
                {
                    if (account.AccountType.ConvertToString() == roleName)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Проверить аккаунт на принадлежность к роли
        /// </summary>
        /// <param name="username"></param>
        /// <param name="accountType"></param>
        /// <returns></returns>
        public bool IsUserInRole(string username, AccountType accountType)
        {
            if (!string.IsNullOrEmpty(username))
            {
                Account account = AccountRepository.GetByLogin(username);
                if (account != null)
                {
                    if (account.AccountType == accountType)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}